home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / PROGRAMMING / PERL.SPK / Perl5001 / Manual / perlrun_ht < prev    next >
Text File  |  1995-04-18  |  17KB  |  449 lines

  1. <!-- $RCSfile$$Revision$$Date$ -->
  2. <!-- $Log$ -->
  3. <HTML>
  4. <TITLE> PERLRUN </TITLE>
  5. <h2>NAME</h2>
  6. perlrun - how to execute the Perl interpreter
  7. <p><h2>SYNOPSIS</h2>
  8. <B>perl</B> [switches] filename args
  9. <p><h2>DESCRIPTION</h2>
  10. Upon startup, Perl looks for your script in one of the following
  11. places:
  12. <p>
  13. <dl>
  14.  
  15. <dt><b><A NAME="perlcall_48">1.</A></b>
  16. <dd>
  17. Specified line by line via 
  18. <A HREF="perlrun.html#perlrun_348">-e</A>
  19.  switches on the command line.
  20. <p></dd>
  21.  
  22. <dt><b><A NAME="perlcall_49">2.</A></b>
  23. <dd>
  24. Contained in the file specified by the first filename on the command line.
  25. (Note that systems supporting the #! notation invoke interpreters this way.)
  26. <p></dd>
  27.  
  28. <dt><b><A NAME="perlcall_50">3.</A></b>
  29. <dd>
  30. Passed in implicitly via standard input.  This only works if there are
  31. no filename arguments--to pass arguments to a STDIN script you
  32. must explicitly specify a "-" for the script name.
  33. <p></dd>
  34.  
  35. </dl>
  36.  
  37. With methods 2 and 3, Perl starts parsing the input file from the
  38. beginning, unless you've specified a 
  39. <A HREF="perlrun.html#perlrun_363">-x</A>
  40.  switch, in which case it
  41. scans for the first line starting with #! and containing the word
  42. "perl", and starts there instead.  This is useful for running a script
  43. embedded in a larger message.  (In this case you would indicate the end
  44. of the script using the __END__ token.)
  45. <p>As of Perl 5, the #! line is always examined for switches as the line is
  46. being parsed.  Thus, if you're on a machine that only allows one argument
  47. with the #! line, or worse, doesn't even recognize the #! line, you still
  48. can get consistent switch behavior regardless of how Perl was invoked,
  49. even if 
  50. <A HREF="perlrun.html#perlrun_363">-x</A>
  51.  was used to find the beginning of the script.
  52. <p>Because many operating systems silently chop off kernel interpretation of
  53. the #! line after 32 characters, some switches may be passed in on the
  54. command line, and some may not; you could even get a "-" without its
  55. letter, if you're not careful.  You probably want to make sure that all
  56. your switches fall either before or after that 32 character boundary.
  57. Most switches don't actually care if they're processed redundantly, but
  58. getting a - instead of a complete switch could cause Perl to try to
  59. execute standard input instead of your script.  And a partial 
  60. <A HREF="perlrun.html#perlrun_351">-I</A>
  61.  switch
  62. could also cause odd results.
  63. <p>Parsing of the #! switches starts wherever "perl" is mentioned in the line.
  64. The sequences "-*" and "- " are specifically ignored so that you could,
  65. if you were so inclined, say
  66. <p><pre>
  67.         #!/bin/sh -- # -*- perl -*- -p
  68.         eval 'exec perl $0 -S ${1+"$@"}'
  69.         if 0;
  70. </pre>
  71. to let Perl see the 
  72. <A HREF="perlrun.html#perlrun_354">-p</A>
  73.  switch.
  74. <p>If the #! line does not contain the word "perl", the program named after
  75. the #! is executed instead of the Perl interpreter.  This is slightly
  76. bizarre, but it helps people on machines that don't do #!, because they
  77. can tell a program that their SHELL is /usr/bin/perl, and Perl will then
  78. dispatch the program to the correct interpreter for them.
  79. <p>After locating your script, Perl compiles the entire script to an
  80. internal form.  If there are any compilation errors, execution of the
  81. script is not attempted.  (This is unlike the typical shell script,
  82. which might run partway through before finding a syntax error.)
  83. <p>If the script is syntactically correct, it is executed.  If the script
  84. runs off the end without hitting an exit() or die() operator, an implicit
  85. <B>exit(0)</B> is provided to indicate successful completion.
  86. <p><h3>Switches</h3>
  87. A single-character switch may be combined with the following switch, if
  88. any.
  89. <p><pre>
  90.         #!/usr/bin/perl -spi.bak        # same as -s -p -i.bak
  91. </pre>
  92. Switches include:
  93. <p>
  94. <dl>
  95. <dt><B><A NAME="perlrun_343"> -0</A> <I>digits</I></B>
  96. <dd>
  97. specifies the record separator (
  98. <A HREF="perlvar.html#perlvar_390">$/</A>
  99. ) as an octal number.  If there are
  100. no digits, the null character is the separator.  Other switches may
  101. precede or follow the digits.  For example, if you have a version of
  102. <B>find</B> which can print filenames terminated by the null character, you
  103. can say this:
  104. <p></dd>
  105. <pre>
  106.         find . -name '*.bak' -print0 | perl -n0e unlink
  107. </pre>
  108. The special value 00 will cause Perl to slurp files in paragraph mode.
  109. The value 0777 will cause Perl to slurp files whole since there is no
  110. legal character with that value.
  111. <p><dt><B><A NAME="perlrun_344"> -a</A> </B>
  112. <dd>
  113. turns on autosplit mode when used with a 
  114. <A HREF="perlrun.html#perlrun_353">-n</A>
  115.  or 
  116. <A HREF="perlrun.html#perlrun_354">-p</A>
  117. .  An implicit
  118. split command to the @F array is done as the first thing inside the
  119. implicit while loop produced by the 
  120. <A HREF="perlrun.html#perlrun_353">-n</A>
  121.  or 
  122. <A HREF="perlrun.html#perlrun_354">-p</A>
  123. .
  124. <p></dd>
  125. <pre>
  126.         perl -ane 'print pop(@F), "\n";'
  127. </pre>
  128. is equivalent to
  129. <p><pre>
  130.         while (<>) {
  131.         @F = split(' ');
  132.         print pop(@F), "\n";
  133.         }
  134. </pre>
  135. An alternate delimiter may be specified using 
  136. <A HREF="perlrun.html#perlrun_349">-F</A>
  137. .
  138. <p><dt><B><A NAME="perlrun_345"> -c</A> </B>
  139. <dd>
  140. causes Perl to check the syntax of the script and then exit without
  141. executing it.
  142. <p></dd>
  143. <dt><B><A NAME="perlrun_346"> -d</A> </B>
  144. <dd>
  145. runs the script under the Perl debugger.  See 
  146. <A HREF="perldebug.html">
  147. the perldebug manpage</A>
  148. .
  149. <p></dd>
  150. <dt><B><A NAME="perlrun_347"> -D</A> <I>number</I></B>
  151. <dt><B><A NAME="perlrun_347"> -D</A> <I>list</I></B>
  152. <dd>
  153. sets debugging flags.  To watch how it executes your script, use
  154. <B>-D14</B>.  (This only works if debugging is compiled into your
  155. Perl.)  Another nice value is <B>-D1024</B>, which lists your compiled
  156. syntax tree.  And <B>-D512</B> displays compiled regular expressions. As an
  157. alternative specify a list of letters instead of numbers (e.g. <B>-D14</B> is
  158. equivalent to <B>-Dtls</B>):
  159. <p></dd>
  160. <pre>
  161.             1  p  Tokenizing and Parsing
  162.             2  s  Stack Snapshots
  163.             4  l  Label Stack Processing
  164.             8  t  Trace Execution
  165.            16  o  Operator Node Construction
  166.            32  c  String/Numeric Conversions
  167.            64  P  Print Preprocessor Command for -P
  168.           128  m  Memory Allocation
  169.           256  f  Format Processing
  170.           512  r  Regular Expression Parsing
  171.          1024  x  Syntax Tree Dump
  172.          2048  u  Tainting Checks
  173.          4096  L  Memory Leaks (not supported anymore)
  174.          8192  H  Hash Dump -- usurps values()
  175.         16384  X  Scratchpad Allocation
  176.         32768  D  Cleaning Up
  177. </pre>
  178. <dt><B><A NAME="perlrun_348"> -e</A>  <I>commandline</I></B>
  179. <dd>
  180. may be used to enter one line of script.  
  181. If 
  182. <A HREF="perlrun.html#perlrun_348">-e</A>
  183.  is given, Perl
  184. will not look for a script filename in the argument list.  
  185. Multiple 
  186. <A HREF="perlrun.html#perlrun_348">-e</A>
  187.  commands may
  188. be given to build up a multi-line script.  
  189. Make sure to use semicolons where you would in a normal program.
  190. <p></dd>
  191. <dt><B><A NAME="perlrun_349"> -F</A> <I>regexp</I></B>
  192. <dd>
  193. specifies a regular expression to split on if 
  194. <A HREF="perlrun.html#perlrun_344">-a</A>
  195.  is also in effect.
  196. If regexp has <B>//</B> around it, the slashes will be ignored.
  197. <p></dd>
  198. <dt><B><A NAME="perlrun_350"> -i</A> <I>extension</I></B>
  199. <dd>
  200. specifies that files processed by the <B><></B> construct are to be edited
  201. in-place.  It does this by renaming the input file, opening the output
  202. file by the original name, and selecting that output file as the default
  203. for print() statements.  The extension, if supplied, is added to the name
  204. of the old file to make a backup copy.  If no extension is supplied, no
  205. backup is made.  From the shell, saying
  206. <p></dd>
  207. <pre>
  208.         $ perl -p -i.bak -e "s/foo/bar/; ... "
  209. </pre>
  210. is the same as using the script:
  211. <p><pre>
  212.         #!/usr/bin/perl -pi.bak
  213.         s/foo/bar/;
  214. </pre>
  215. which is equivalent to
  216. <p><pre>
  217.         #!/usr/bin/perl
  218.         while (<>) {
  219.         if ($ARGV ne $oldargv) {
  220.             rename($ARGV, $ARGV . '.bak');
  221.             open(ARGVOUT, ">$ARGV");
  222.             select(ARGVOUT);
  223.             $oldargv = $ARGV;
  224.         }
  225.         s/foo/bar/;
  226.         }
  227.         continue {
  228.         print;  # this prints to original filename
  229.         }
  230.         select(STDOUT);
  231. </pre>
  232. except that the 
  233. <A HREF="perlrun.html#perlrun_350">-i</A>
  234.  form doesn't need to compare $ARGV to $oldargv to
  235. know when the filename has changed.  It does, however, use ARGVOUT for
  236. the selected filehandle.  Note that STDOUT is restored as the
  237. default output filehandle after the loop.
  238. <p>You can use 
  239. <A HREF="perlfunc.html#perlfunc_100">eof</A>
  240.  without parenthesis to locate the end of each input file, 
  241. in case you want to append to each file, or reset line numbering (see 
  242. example in perlfunc/eof).
  243. <p><dt><B><A NAME="perlrun_351"> -I</A> <I>directory</I></B>
  244. <dd>
  245. may be used in conjunction with 
  246. <A HREF="perlrun.html#perlrun_355">-P</A>
  247.  to tell the C preprocessor where
  248. to look for include files.  By default /usr/include and /usr/lib/perl
  249. are searched.
  250. <p></dd>
  251. <dt><B><A NAME="perlrun_352"> -l</A> <I>octnum</I></B>
  252. <dd>
  253. enables automatic line-ending processing.  It has two effects:  first,
  254. it automatically chomps the line terminator when used with 
  255. <A HREF="perlrun.html#perlrun_353">-n</A>
  256.  or
  257.  
  258. <A HREF="perlrun.html#perlrun_354">-p</A>
  259. , and second, it assigns "
  260. <A HREF="perlvar.html#perlvar_401">$\</A>
  261. " to have the value of <I>octnum</I> so that
  262. any print statements will have that line terminator added back on.  If
  263. <I>octnum</I> is omitted, sets "
  264. <A HREF="perlvar.html#perlvar_401">$\</A>
  265. " to the current value of "
  266. <A HREF="perlvar.html#perlvar_390">$/</A>
  267. ".  For
  268. instance, to trim lines to 80 columns:
  269. <p></dd>
  270. <pre>
  271.         perl -lpe 'substr($_, 80) = ""'
  272. </pre>
  273. Note that the assignment <B>$\ = $/</B> is done when the switch is processed,
  274. so the input record separator can be different than the output record
  275. separator if the 
  276. <A HREF="perlrun.html#perlrun_352">-l</A>
  277.  switch is followed by a 
  278. <A HREF="perlrun.html#perlrun_343">-0</A>
  279.  switch:
  280. <p><pre>
  281.         gnufind / -print0 | perl -ln0e 'print "found $_" if -p'
  282. </pre>
  283. This sets $\ to newline and then sets $/ to the null character.
  284. <p><dt><B><A NAME="perlrun_353"> -n</A> </B>
  285. <dd>
  286. causes Perl to assume the following loop around your script, which
  287. makes it iterate over filename arguments somewhat like <B>sed -n</B> or
  288. <B>awk</B>:
  289. <p></dd>
  290. <pre>
  291.         while (<>) {
  292.         ...             # your script goes here
  293.         }
  294. </pre>
  295. Note that the lines are not printed by default.  See 
  296. <A HREF="perlrun.html#perlrun_354">-p</A>
  297.  to have
  298. lines printed.  Here is an efficient way to delete all files older than
  299. a week:
  300. <p><pre>
  301.         find . -mtime +7 -print | perl -nle 'unlink;'
  302. </pre>
  303. This is faster than using the <B>-exec</B> switch of <B>find</B> because you don't
  304. have to start a process on every filename found.
  305. <p><B>BEGIN</B> and <B>END</B> blocks may be used to capture control before or after
  306. the implicit loop, just as in <B>awk</B>.
  307. <p><dt><B><A NAME="perlrun_354"> -p</A> </B>
  308. <dd>
  309. causes Perl to assume the following loop around your script, which
  310. makes it iterate over filename arguments somewhat like <B>sed</B>:
  311. <p></dd>
  312. <pre>
  313.         while (<>) {
  314.         ...             # your script goes here
  315.         } continue {
  316.         print;
  317.         }
  318. </pre>
  319. Note that the lines are printed automatically.  To suppress printing
  320. use the 
  321. <A HREF="perlrun.html#perlrun_353">-n</A>
  322.  switch.  A 
  323. <A HREF="perlrun.html#perlrun_354">-p</A>
  324.  overrides a 
  325. <A HREF="perlrun.html#perlrun_353">-n</A>
  326.  switch.
  327. <p><B>BEGIN</B> and <B>END</B> blocks may be used to capture control before or after
  328. the implicit loop, just as in awk.
  329. <p><dt><B><A NAME="perlrun_355"> -P</A> </B>
  330. <dd>
  331. causes your script to be run through the C preprocessor before
  332. compilation by Perl.  (Since both comments and cpp directives begin
  333. with the # character, you should avoid starting comments with any words
  334. recognized by the C preprocessor such as "if", "else" or "define".)
  335. <p></dd>
  336. <dt><B><A NAME="perlrun_356"> -s</A> </B>
  337. <dd>
  338. enables some rudimentary switch parsing for switches on the command
  339. line after the script name but before any filename arguments (or before
  340. a <B>--</B>).  Any switch found there is removed from @ARGV and sets the
  341. corresponding variable in the Perl script.  The following script
  342. prints "true" if and only if the script is invoked with a <B>-xyz</B> switch.
  343. <p></dd>
  344. <pre>
  345.         #!/usr/bin/perl -s
  346.         if ($xyz) { print "true\n"; }
  347. </pre>
  348. <dt><B><A NAME="perlrun_357"> -S</A> </B>
  349. <dd>
  350. makes Perl use the PATH environment variable to search for the
  351. script (unless the name of the script starts with a slash).  Typically
  352. this is used to emulate #! startup on machines that don't support #!,
  353. in the following manner:
  354. <p></dd>
  355. <pre>
  356.         #!/usr/bin/perl
  357.         eval "exec /usr/bin/perl -S $0 $*"
  358.             if $running_under_some_shell;
  359. </pre>
  360. The system ignores the first line and feeds the script to /bin/sh,
  361. which proceeds to try to execute the Perl script as a shell script.
  362. The shell executes the second line as a normal shell command, and thus
  363. starts up the Perl interpreter.  On some systems $0 doesn't always
  364. contain the full pathname, so the 
  365. <A HREF="perlrun.html#perlrun_357">-S</A>
  366.  tells Perl to search for the
  367. script if necessary.  After Perl locates the script, it parses the
  368. lines and ignores them because the variable $running_under_some_shell
  369. is never true.  A better construct than 
  370. <A HREF="perlvar.html#perlvar_382">$*</A>
  371.  would be <B>${1+"$@"}</B>, which
  372. handles embedded spaces and such in the filenames, but doesn't work if
  373. the script is being interpreted by csh.  In order to start up sh rather
  374. than csh, some systems may have to replace the #! line with a line
  375. containing just a colon, which will be politely ignored by Perl.  Other
  376. systems can't control that, and need a totally devious construct that
  377. will work under any of csh, sh or Perl, such as the following:
  378. <p><pre>
  379.         eval '(exit $?0)' && eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
  380.         & eval 'exec /usr/bin/perl -S $0 $argv:q'
  381.                 if 0;
  382. </pre>
  383. <dt><B><A NAME="perlrun_358"> -T</A> </B>
  384. <dd>
  385. forces "taint" checks to be turned on.  Ordinarily these checks are
  386. done only when running setuid or setgid.  See 
  387. <A HREF="perlsec.html">
  388. the perlsec manpage</A>
  389. .
  390. <p></dd>
  391. <dt><B><A NAME="perlrun_359"> -u</A> </B>
  392. <dd>
  393. causes Perl to dump core after compiling your script.  You can then
  394. take this core dump and turn it into an executable file by using the
  395. <B>undump</B> program (not supplied).  This speeds startup at the expense of
  396. some disk space (which you can minimize by stripping the executable).
  397. (Still, a "hello world" executable comes out to about 200K on my
  398. machine.)  If you want to execute a portion of your script before dumping,
  399. use the dump() operator instead.  Note: availability of <B>undump</B> is
  400. platform specific and may not be available for a specific port of
  401. Perl.
  402. <p></dd>
  403. <dt><B><A NAME="perlrun_360"> -U</A> </B>
  404. <dd>
  405. allows Perl to do unsafe operations.  Currently the only "unsafe"
  406. operations are the unlinking of directories while running as superuser,
  407. and running setuid programs with fatal taint checks turned into
  408. warnings.
  409. <p></dd>
  410. <dt><B><A NAME="perlrun_361"> -v</A> </B>
  411. <dd>
  412. prints the version and patchlevel of your Perl executable.
  413. <p></dd>
  414. <dt><B><A NAME="perlrun_362"> -w</A> </B>
  415. <dd>
  416. prints warnings about identifiers that are mentioned only once, and
  417. scalar variables that are used before being set.  Also warns about
  418. redefined subroutines, and references to undefined filehandles or
  419. filehandles opened readonly that you are attempting to write on.  Also
  420. warns you if you use values as a number that doesn't look like numbers, using
  421. a an array as though it were a scalar, if
  422. your subroutines recurse more than 100 deep, and innumeriable other things.
  423. See 
  424. <A HREF="perldiag.html">
  425. the perldiag manpage</A>
  426.  and 
  427. <A HREF="perltrap.html">
  428. the perltrap manpage</A>
  429. .
  430. <p></dd>
  431. <dt><B><A NAME="perlrun_363"> -x</A>  <I>directory</I></B>
  432. <dd>
  433. tells Perl that the script is embedded in a message.  Leading
  434. garbage will be discarded until the first line that starts with #! and
  435. contains the string "perl".  Any meaningful switches on that line will
  436. be applied (but only one group of switches, as with normal #!
  437. processing).  If a directory name is specified, Perl will switch to
  438. that directory before running the script.  The 
  439. <A HREF="perlrun.html#perlrun_363">-x</A>
  440.  switch only
  441. controls the the disposal of leading garbage.  The script must be
  442. terminated with <B>__END__</B> if there is trailing garbage to be ignored (the
  443. script can process any or all of the trailing garbage via the DATA
  444. filehandle if desired).
  445. <p></dd>
  446.  
  447. </dl>
  448.  
  449.